
public class GameDataMgr
{
    private static GameDataMgr instance = new GameDataMgr();
    public static GameDataMgr Instance => instance;
    public SoundData soundData;
    public RankList rankData;
    private GameDataMgr()
    {
        //进入游戏加载数据
        soundData = PlayerPrefsDataMgr.Instance.LoadData(typeof(SoundData), "Sound") as SoundData;
        //如果是第一次加载数据，那就初始化数据并保存
        if (!soundData.notFirst)
        {
            soundData.isOnMusic = true;
            soundData.isOnSoundEffect = true;
            soundData.musicValue = 1;
            soundData.soundEffectValue = 1;
            soundData.notFirst = true;
            PlayerPrefsDataMgr.Instance.SaveData(soundData, "Sound");
        }
        //读取排行数据
        rankData = PlayerPrefsDataMgr.Instance.LoadData(typeof(RankList), "Rank") as RankList;

    }
    //提供给外部添加排行数据的方法
    public void AddRankInfo(RankInfo rankInfo)
    {
        // //方法1：遍历比较后决定添加在数轴的位置
        // //遍历所有，如果时间小则插入，否则加到末尾
        // for (int i = 0; i < rankData.Count; i++)
        // {
        //     if (rankInfo.time < rankData[i].time)
        //     {
        //         rankData.Insert(i, rankInfo);
        //         return;
        //     }
        // }
        // //添加到末尾
        // rankData.Add(rankInfo);

        //方法2：添加后Sort排序
        rankData.list.Add(rankInfo);
        //比较器：a<b则返回-1，表示a放在b的前边
        rankData.list.Sort((a, b) => a.time < b.time ? -1 : 1);

        //移除前10以外的数据
        if (rankData.list.Count > 10)
            rankData.list.RemoveRange(10, rankData.list.Count - 10);
        //存储
        PlayerPrefsDataMgr.Instance.SaveData(rankData, "Rank");
    }
}
